﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DNA
{
    class regres_for_tree:tree
    {
        public int A = 1;
        public int G = 2;
        public int T = 3;
        public int C = 4;
        public int D = 5;
        public int I = 6;
        int koren = 0;
        int where_y;
        int numberofequel;
       
        public int new_main_koren(string our_y)
        {
            preobrazvat();
            clear();
            tree our_tree = this.Owner as tree;
            numberofequel = (int)our_tree.eq_count.Value;
            whetre_my_y(our_y);
            summ_y();
           summ_x();
           shumel_method();
           luthee();
           vernyt();
           return koren;
        }


        public void clear()
        {
            tree our_tree = this.Owner as tree;
            //for (int roww = 0; roww < our_tree.calcdata.Rows.Count - 1; roww++)
            //{
            our_tree.calcdata.Rows.Clear();
           /// }
        }

        public void luthee()
        { 
            tree our_tree = this.Owner as tree;
            double znach = (double)our_tree.calcdata[1, 1].Value;
            string luth = our_tree.calcdata[0, 1].Value.ToString();
          for (int roww = 2; roww < our_tree.calcdata.Rows.Count - 1; roww++)
          {
              if (znach < (double)our_tree.calcdata[1, roww].Value)
              {
                  znach = (double)our_tree.calcdata[1, roww].Value;
                  luth = our_tree.calcdata[0, roww].Value.ToString();
              }
          }
          for (int col = 0; col < our_tree.data.Columns.Count; col++)
          {
              if (luth == our_tree.data.Columns[col].HeaderText)
              {
                  koren = col;
                  break;
              }
              else continue;
          }
        }

        private void whetre_my_y(string wht_search)
        {
            tree our_tree = this.Owner as tree;
            for (int i = 0; i < our_tree.data.Columns.Count; i++)
            {
                if (our_tree.data.Columns[i].HeaderText == wht_search)
                {
                    where_y = i;
                    break;
                }
                else
                { continue; }
            }
        }
        
        private void summ_y()
        {
            float summ1 = 0;
            tree our_tree = this.Owner as tree;
            for (int i = 0; i < numberofequel; i++)
            {
                summ1 = summ1 + Convert.ToInt32(our_tree .data[where_y, i].Value);
            }
            summ1 = summ1 / (numberofequel);
            our_tree.calcdata.Rows.Add('y', summ1);
        }


        private void summ_x()
        {
            string name;
            double summ1 = 0;
            tree our_tree = this.Owner as tree;
            for (int i = 0; i < our_tree.data.Columns.Count; i++)
            {
                if (i != where_y)
                {
                    for (int j = 0; j < numberofequel; j++)
                    {
                        summ1 = summ1 + Convert.ToInt32(our_tree.data[i, j].Value);
                    }
                    name = our_tree.data.Columns[i].HeaderText;
                    summ1 = summ1 / (numberofequel);
                    our_tree.calcdata.Rows.Add(name, summ1);
                    name = "";
                    summ1 = 0;
                }
                else { continue; }
            }

        }


        private void shumel_method()
        {
            double summ1 = 0;
            double summ2 = 0;
            double sred_x = 0;
            Int32 where_x = 0;
            string name_x;
             tree our_tree = this.Owner as tree;
            double sred_y = Convert.ToDouble(our_tree.calcdata[1, 0].Value);
            try
            {
                for (int i = 1; i < our_tree.calcdata.Rows.Count - 1; i++)
                {
                    sred_x = Convert.ToDouble(our_tree.calcdata[1, i].Value);
                    name_x = Convert.ToString(our_tree.calcdata[0, i].Value);
                    
                    for (int z = 0; z < our_tree.data.ColumnCount; z++)
                    {
                        if (name_x == our_tree.data.Columns[z].HeaderText)
                        {
                            where_x = z;
                        }
                    }
                    for (int j = 0; j < numberofequel; j++)
                    {
                        summ1 = summ1 + ((Convert.ToDouble(our_tree.data[where_x, j].Value) - sred_x) * ((Convert.ToDouble(our_tree.data[where_y, j].Value)) - sred_y));
                        summ2 = summ2 + Math.Pow((Convert.ToDouble(our_tree.data[where_x, j].Value) - sred_x), 2);
                    }
                    summ1 = summ1 / summ2;
                    our_tree.calcdata[1, i].Value = summ1;
                    summ1 = 0;
                    summ2 = 0;
                }
            }
            catch
            {
                
            }

        }

        private void preobrazvat()
        {
            tree our_tree = this.Owner as tree;
            for (int i = 0; i < our_tree.data.Columns.Count; i++)
            {
                for (int j = 0; j < our_tree.data.Rows.Count; j++)
                {
                    try
                    {
                        if (our_tree.data[i, j].Value.ToString() == "отрицат")
                        {
                            our_tree.data[i, j].Value = "0";
                        }
                        if (our_tree.data[i, j].Value.ToString() == "полож")
                        {
                            our_tree.data[i, j].Value = "1";
                        }
                        if (our_tree.data[i, j].Value.ToString() == "М")
                        {
                            our_tree.data[i, j].Value = "3";
                        }
                        if (our_tree.data[i, j].Value.ToString() == "Ж")
                        {
                            our_tree.data[i, j].Value = "4";
                        }
                        if (our_tree.data[i, j].Value.ToString() == "AA")
                        {
                            our_tree.data[i, j].Value = A.ToString() + A.ToString();
                        }
                        if (our_tree.data[i, j].Value.ToString() == "AG")
                        {
                            our_tree.data[i, j].Value = A.ToString() + G.ToString();
                        }
                        if (our_tree.data[i, j].Value.ToString() == "AC")
                        {
                            our_tree.data[i, j].Value = A.ToString() + C.ToString();
                        }
                        if (our_tree.data[i, j].Value.ToString() == "GG")
                        {
                            our_tree.data[i, j].Value = G.ToString() + G.ToString();
                        }
                        if (our_tree.data[i, j].Value.ToString() == "GA")
                        {
                            our_tree.data[i, j].Value = G.ToString() + A.ToString();
                        }
                        if (our_tree.data[i, j].Value.ToString() == "GT")
                        {
                            our_tree.data[i, j].Value = G.ToString() + T.ToString();
                        }
                        if (our_tree.data[i, j].Value.ToString() == "GC")
                        {
                            our_tree.data[i, j].Value = G.ToString() + C.ToString();
                        }
                        if (our_tree.data[i, j].Value.ToString() == "TC")
                        {
                            our_tree.data[i, j].Value = T.ToString() + C.ToString();
                        }
                        if (our_tree.data[i, j].Value.ToString() == "TG")
                        {
                            our_tree.data[i, j].Value = T.ToString() + G.ToString();
                        }
                        if (our_tree.data[i, j].Value.ToString() == "CA")
                        {
                            our_tree.data[i, j].Value = C.ToString() + A.ToString();
                        }
                        if (our_tree.data[i, j].Value.ToString() == "CG")
                        {
                            our_tree.data[i, j].Value = C.ToString() + G.ToString();
                        }
                        if (our_tree.data[i, j].Value.ToString() == "CT")
                        {
                            our_tree.data[i, j].Value = C.ToString() + T.ToString();
                        }
                        if (our_tree.data[i, j].Value.ToString() == "TT")
                        {
                            our_tree.data[i, j].Value = T.ToString() + T.ToString();
                        }
                        if (our_tree.data[i, j].Value.ToString() == "CC")
                        {
                            our_tree.data[i, j].Value = C.ToString() + C.ToString();
                        }
                        if (our_tree.data[i, j].Value.ToString() == "DD")
                        {
                            our_tree.data[i, j].Value = D.ToString() + D.ToString();
                        }
                        if (our_tree.data[i, j].Value.ToString() == "II")
                        {
                            our_tree.data[i, j].Value = I.ToString() + I.ToString();
                        }
                        if (our_tree.data[i, j].Value.ToString() == "ID")
                        {
                            our_tree.data[i, j].Value = I.ToString() + D.ToString();
                        }
                    }
                    catch
                    { continue; }
                }
            }
        }

        private void vernyt()
        {
            tree our_tree = this.Owner as tree;
            for (int i = 0; i < our_tree.data.Columns.Count; i++)
            {
                for (int j = 0; j < our_tree.data.Rows.Count; j++)
                {
                    try
                    {
                        if (our_tree.data.Columns[i].HeaderText == "ожирение" || our_tree.data.Columns[i].HeaderText == "курение" || our_tree.data.Columns[i].HeaderText == "алкоголь" || our_tree.data.Columns[i].HeaderText == "возраст") continue;
                        if (our_tree.data[i, j].Value.ToString() == "0")
                        {
                            our_tree.data[i, j].Value = "отрицат";
                        }
                        if (our_tree.data[i, j].Value.ToString() == "1")
                        {
                            our_tree.data[i, j].Value = "полож";
                        }
                        if (our_tree.data[i, j].Value.ToString() == "3")
                        {
                            our_tree.data[i, j].Value = "М";
                        }
                        if (our_tree.data[i, j].Value.ToString() == "4")
                        {
                            our_tree.data[i, j].Value = "Ж";
                        }
                        if (our_tree.data[i, j].Value.ToString() == "11")
                        {
                            our_tree.data[i, j].Value = "AA";
                        }
                        if (our_tree.data[i, j].Value.ToString() == "12")
                        {
                            our_tree.data[i, j].Value = "AG";
                        }
                        if (our_tree.data[i, j].Value.ToString() == "14")
                        {
                            our_tree.data[i, j].Value = "AC";
                        }
                        if (our_tree.data[i, j].Value.ToString() == "22")
                        {
                            our_tree.data[i, j].Value = "GG";
                        }
                        if (our_tree.data[i, j].Value.ToString() == "21")
                        {
                            our_tree.data[i, j].Value = "GA";
                        }
                        if (our_tree.data[i, j].Value.ToString() == "23")
                        {
                            our_tree.data[i, j].Value = "GT";
                        }
                        if (our_tree.data[i, j].Value.ToString() == "24")
                        {
                            our_tree.data[i, j].Value = "GC";
                        }
                        if (our_tree.data[i, j].Value.ToString() == "34")
                        {
                            our_tree.data[i, j].Value = "TC";
                        }
                        if (our_tree.data[i, j].Value.ToString() == "32")
                        {
                            our_tree.data[i, j].Value = "TG";
                        }
                        if (our_tree.data[i, j].Value.ToString() == "41")
                        {
                            our_tree.data[i, j].Value = "CA";
                        }
                        if (our_tree.data[i, j].Value.ToString() == "42")
                        {
                            our_tree.data[i, j].Value = "CG";
                        }
                        if (our_tree.data[i, j].Value.ToString() == "43")
                        {
                            our_tree.data[i, j].Value = "CT";
                        }
                        if (our_tree.data[i, j].Value.ToString() == "33")
                        {
                            our_tree.data[i, j].Value = "TT";
                        }
                        if (our_tree.data[i, j].Value.ToString() == "44")
                        {
                            our_tree.data[i, j].Value = "CC";
                        }
                        if (our_tree.data[i, j].Value.ToString() == "55")
                        {
                            our_tree.data[i, j].Value = "DD";
                        }
                        if (our_tree.data[i, j].Value.ToString() == "66")
                        {
                            our_tree.data[i, j].Value = "II";
                        }
                        if (our_tree.data[i, j].Value.ToString() == "65")
                        {
                            our_tree.data[i, j].Value = "ID";
                        }
                    }
                    catch
                    { continue; }
                }
            }
        }
    }
}
